/*
 * Copyright 2008-2009 Daniel Freitas
 * 
 * This file is part of Army Creator.
 * 
 * Army Creator is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * Army Creator is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with Army Creator. If not, see
 * <http://www.gnu.org/licenses/>.
 * 
 * This code is based on the work of Paul Szulc.
 * 
 * Please check http://paulszulc.wordpress.com/2009/08/05/enhanced-wickettester/
 */
package com.googlecode.armycreator.enhancedwickettester;

import static org.testng.Assert.assertTrue;

import org.apache.wicket.Component;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;

public class EnhancedFormTester {

    private final FormTester formTester;
    private final WicketTester tester;
    private final String formPath;

    public EnhancedFormTester(WicketTester tester, String formPath) {
        this(tester, tester.newFormTester(formPath), formPath);
    }

    public EnhancedFormTester(WicketTester tester, FormTester formTester, String formPath) {
        this.formTester = formTester;
        this.tester = tester;
        this.formPath = formPath;
    }

    public EnhancedFormTester selectDropDownChoice(String path, int index) {
        String fullPath = formPath + ":" + path;

        tester.assertComponent(fullPath, DropDownChoice.class);
        tester.assertVisible(fullPath);
        Component dropDownChoice = tester.getComponentFromLastRenderedPage(fullPath);
        assertTrue(dropDownChoice.isEnabled());
        formTester.select(path, index);

        return new EnhancedFormTester(tester, formTester, formPath);
    }

    public EnhancedFormTester setTextFieldValue(String path, String value) {
        String fullPath = formPath + ":" + path;

        tester.assertComponent(fullPath, TextField.class);
        tester.assertVisible(fullPath);
        Component textField = tester.getComponentFromLastRenderedPage(fullPath);
        assertTrue(textField.isEnabled());
        formTester.setValue(path, value);

        return new EnhancedFormTester(tester, formTester, formPath);
    }

    public EnhancedFormTester setTextAreaValue(String path, String value) {
        String fullPath = formPath + ":" + path;

        tester.assertComponent(fullPath, TextArea.class);
        tester.assertVisible(fullPath);
        Component textField = tester.getComponentFromLastRenderedPage(fullPath);
        assertTrue(textField.isEnabled());
        formTester.setValue(path, value);

        return new EnhancedFormTester(tester, formTester, formPath);
    }

    public EnhancedFormTester setPasswordTextFieldValue(String path, String value) {
        String fullPath = formPath + ":" + path;

        tester.assertComponent(fullPath, PasswordTextField.class);
        tester.assertVisible(fullPath);
        Component textField = tester.getComponentFromLastRenderedPage(fullPath);
        assertTrue(textField.isEnabled());
        formTester.setValue(path, value);

        return new EnhancedFormTester(tester, formTester, formPath);
    }

    public void submitWithButton(String path) {
        String fullPath = formPath + ":" + path;

        tester.assertComponent(fullPath, Button.class);
        tester.assertVisible(fullPath);
        Component button = tester.getComponentFromLastRenderedPage(fullPath);
        assertTrue(button.isEnabled());
        formTester.submit(path);
    }

    public void submitWithSubmitLink(String path) {
        tester.assertComponent(formPath + ":" + path, SubmitLink.class);
        tester.assertVisible(formPath + ":" + path);
        Component button = tester.getComponentFromLastRenderedPage(formPath + ":" + path);
        assertTrue(button.isEnabled());
        formTester.submit(path);
    }

    public EnhancedFormTester executeAjaxEvent(String component, String event) {
        tester.executeAjaxEvent(formPath + ":" + component, event);
        return new EnhancedFormTester(tester, formTester, formPath);
    }
}
